package com.tuling.mall.user.loadbalancer.controller;

import com.tuling.mall.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

//    @Autowired
//    private WebClient webClient;
//
//    @Autowired
//    private ReactorLoadBalancerExchangeFilterFunction lbFunction;

    @RequestMapping("/findOrder/{userId}")
    public R findOrder(@PathVariable("userId") Integer userId) {
        String url = "http://mall-order/order/findOrderByUserId/" + userId;
        R object = restTemplate.getForObject(url, R.class);

        System.out.println("object = " + object);

        return object;
    }


//    @RequestMapping("/findOrder2/{userId}")
//    public Mono<R> findOrder2(@PathVariable("userId") Integer userId) {
//        String url = "http://mall-order/order/findOrderByUserId/" + userId;
//
//
//        //基于WebClient
//        Mono<R> result = webClient.get().uri(url).retrieve().bodyToMono(R.class);
//
//        System.out.println("result = " + result);
//        return result;
//    }
//
//    @RequestMapping("/findOrder3/{userId}")
//    public Mono<R> findOrder3(@PathVariable("userId") Integer userId) {
//        String url = "http://mall-order/order/findOrderByUserId/" + userId;
//
//
//        //基于WebClient+webFlux
//        Mono<R> result = WebClient.builder()
//                .filter(lbFunction)
//                .build()
//                .get()
//                .uri(url)
//                .retrieve()
//                .bodyToMono(R.class);
//        System.out.println("result = " + result);
//        return result;
//
//    }

}
